#include "core/monitor_manager.h"

#include <iostream>
#include <memory>

#include "config/config_manager.h"
#include "executor/system_tool_executor.h"
#include "executor/system_file_executor.h"
#include <glog/logging.h>

namespace sysinfo {
namespace monitor {

// 定义队列大小常量
static constexpr size_t kSystemInfoQueueSize = 1000;
static constexpr size_t kSystemInfoCircularQueueSize = 100;

MonitorManager::MonitorManager() = default;

MonitorManager::~MonitorManager() { Stop(); }

bool MonitorManager::Init(const std::string& config_file_path) {
  config_manager_ = std::make_unique<ConfigManager>();
  if (!config_manager_->LoadConfig(config_file_path, &config_)) {
    LOG(ERROR) << "Failed to load config: " << config_file_path;
    return false;
  }

  // 检查全局启用配置
  if (!config_.enable_monitor()) {
    LOG(INFO) << "Monitor is disabled by configuration.";
    return false;
  }

  // 有界队列，与老项目保持一致
  system_info_queue_ = std::make_shared<BoundedMonitorQueue<SystemInfoProto>>(kSystemInfoQueueSize);
  // 循环队列，与老项目保持一致
  system_info_circular_queue_ = std::make_shared<CircularMonitorQueue<SystemInfoProto>>(kSystemInfoCircularQueueSize);

  // 初始化执行器
  system_tool_executor_ = std::make_unique<SystemToolExecutor>(
      config_.system_tool_executor(),
      *system_info_queue_,
      *system_info_circular_queue_);

  system_file_executor_ = std::make_unique<SystemFileExecutor>(
      config_.system_file_executor(),
      *system_info_queue_);

  return true;
}

void MonitorManager::Start() {
  // 检查全局启用配置
  if (!config_.enable_monitor()) {
    LOG(INFO) << "Monitor is disabled by configuration.";
    return;
  }

  if (system_tool_executor_) {
    system_tool_executor_->Start();
  }

  if (system_file_executor_) {
    system_file_executor_->Start();
  }
}

void MonitorManager::Stop() {
  if (system_tool_executor_) {
    system_tool_executor_->Stop();
  }

  if (system_file_executor_) {
    system_file_executor_->Stop();
  }

  // 通知队列结束
  if (system_info_queue_) {
    system_info_queue_->SetFinish(true);
    system_info_queue_->NotifyAll();
  }
  
  if (system_info_circular_queue_) {
    system_info_circular_queue_->SetFinish(true);
    system_info_circular_queue_->NotifyAll();
  }
}

void MonitorManager::StartOnce() {
  // 执行一次性系统工具任务
  if (system_tool_executor_) {
    system_tool_executor_->StartOnce();
  }
  
  // SystemFileExecutor不再支持StartOnce方法
}

std::vector<SystemInfoProto> MonitorManager::FetchResultBatchByQueueSize() {
  if (system_info_queue_) {
    size_t batch_size = system_info_queue_->Size();
    if (batch_size > 0) {
      return system_info_queue_->TakeBatch(batch_size);
    }
  }
  return {};
}

std::vector<SystemInfoProto> MonitorManager::FetchResultBatchByQueueSizeFromCircular() {
  if (system_info_circular_queue_) {
    size_t batch_size = system_info_circular_queue_->Size();
    if (batch_size > 0) {
      return system_info_circular_queue_->TakeBatch(batch_size);
    }
  }
  return {};
}

size_t MonitorManager::GetMainQueueSize() const {
  if (system_info_queue_) {
    return system_info_queue_->Size();
  }
  return 0;
}

size_t MonitorManager::GetCircularQueueSize() const {
  if (system_info_circular_queue_) {
    return system_info_circular_queue_->Size();
  }
  return 0;
}

SystemInfoProto MonitorManager::FetchResult() {
  if (system_info_queue_ && !system_info_queue_->Empty()) {
    return system_info_queue_->Take();
  }
  return SystemInfoProto{};
}

std::vector<SystemInfoProto> MonitorManager::FetchResultBatch(size_t batch_size) {
  if (system_info_queue_) {
    return system_info_queue_->TakeBatch(batch_size);
  }
  return {};
}

std::vector<SystemInfoProto> MonitorManager::FetchResultBatchFromCircular(size_t batch_size) {
  if (system_info_circular_queue_) {
    return system_info_circular_queue_->TakeBatch(batch_size);
  }
  return {};
}

}  // namespace monitor
}  // namespace sysinfo